package org.example.volunteerend.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.example.volunteerend.entity.ImageStorage;

@Mapper
public interface ImageStorageMapper {
    
    /**
     * 添加图片记录
     */
    @Insert("INSERT INTO image_storage (user_id, image_url, upload_time, image_type) " +
            "VALUES (#{image.userId}, #{image.imageUrl}, NOW(), #{image.imageType})")
    @Options(useGeneratedKeys = true, keyProperty = "image.imageId")
    void insert(@Param("image") ImageStorage image);
    
    /**
     * 删除图片记录
     */
    @Delete("DELETE FROM image_storage WHERE image_id = #{imageId}")
    int delete(@Param("imageId") Integer imageId);
    
    /**
     * 查询图片记录
     */
    @Select("SELECT * FROM image_storage WHERE image_id = #{imageId}")
    ImageStorage findById(@Param("imageId") Integer imageId);
    
    /**
     * 查询用户图片列表（分页在Service层处理）
     */
    @Select("<script>" +
            "SELECT * FROM image_storage " +
            "WHERE user_id = #{userId} " +
            "<if test='imageType != null and imageType != \"\"'>" +
            "AND image_type = #{imageType} " +
            "</if>" +
            "ORDER BY upload_time DESC" +
            "</script>")
    List<ImageStorage> findByUserId(@Param("userId") Integer userId, @Param("imageType") String imageType);
    
    /**
     * 统计用户图片数量
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM image_storage " +
            "WHERE user_id = #{userId} " +
            "<if test='imageType != null and imageType != \"\"'>" +
            "AND image_type = #{imageType} " +
            "</if>" +
            "</script>")
    int countByUserId(@Param("userId") Integer userId, @Param("imageType") String imageType);
} 